#function(idata,title,window,icon)

#import toolbox;


local obj=toolbox.new_toolbox(idata,title||_hT("Smartdata#toolbox"),window||"toolbox.frame",icon);

local default_option=table();
default_option.showstep=true;

obj.config.default_option=default_option;


obj.operations=[];

obj.load_project=function(model,project,flag)
{
	if(project==nil) return;

	model.data.op_index=project.op_index;
	model.sub_data[this.operations[project.op_index].pdata_index]=clone(project.pdata);
	model.transfer_data(+1);
	model.context.pending_exec(model,"op_index",flag);
};

obj.do_load_config=function(model)
{
	local option=this.config.option;
	local pdata=model.sub_data[this.operations[model.data.op_index].pdata_index];
	
	option.initdata=this.config.make_project(_hT("Default"),pdata,model.data.op_index);
	if(option.lastdata==nil)
	{
		option.lastdata=this.config.make_project(_hT("LastRun"),pdata,model.data.op_index);		
	}
	else
	{
		this.load_project(model,option.lastdata,true);
	}
	
	try
	{	
		model.data.result_figure.attribute=this.config.option.figure_attribute;
	}
	catch()
	{
	
	}
}

obj.do_save_config=function(model)
{
	local pdata=model.sub_data[this.operations[model.data.op_index].pdata_index];
	this.config.option.lastdata=this.config.make_project(_hT("LastRun"),pdata,model.data.op_index);
}
		

obj.pending_exec=function(model,name,flag)
{
	local sd=model.data.sd;
	
	if(name==nil)
	{
		sd.b_pending_exec=false;
		return;
	}
	
	if(name=="")
	{
		if(sd.b_op_executed)
		{
			return;
		}
		sd.b_op_executed=true;
	}
	
	if(sd.b_pending_exec)
	{
		return;
	}
	
	if(flag && this.no_autoexec_on_open)
	{
		return;
	}
	
	model.data.b_pending_exec=true;
	local t=table();t.iwmptr=model;
	
	model.evtmgr["btn.exec"].delayed_cmd_execute(t);
};




obj.new_operation=function(name,label)
{

	local op=toolbox.new_operation(name,label);

	op.lastdata=table();
	op.output=table(); // data export [optional or debugging]
	op.autoexec=false;
	op.autocheck=true;
	
	op.update=function(idata,pdata,model,evtmgr)
	{
		local flag=pdata!=this.lastdata.pdata;
		evtmgr["btn.exec"].set("FLAG_ENABLE",flag);
	};
	
	op.execute=function(idata,pdata,evtmgr)
	{
		local flag = ?(this.autocheck,(this.lastdata.result==nil||pdata!=this.lastdata.pdata),true);
		if (flag)
		{
			this.lastdata.result=[this.do_execute(idata,pdata,evtmgr)];
			this.lastdata.pdata=clone(pdata);
		}
		return unpack(this.lastdata.result);
	};

    op.get_output = function()
    {
        return this.output;
    };

	
	return op;
};

obj.add_operation=function(name,label)
{
	local op=this.new_operation(name,label);
	this.operations.push(op);
	return op;
};

obj.evt_init=function(model)
{

	#import ui;
	
		
	local pending_exec=function(evt,model)
	{
		if(evt=="") return;
		model.context.pending_exec(model,evt);
	};
	
	
	this.basic_evt_init(model,this.operations,pending_exec);
	
	model.data.sd=table();
	model.data.sd.last_result=table();
	model.data.sd.b_op_executed=false;
	model.data.sd.b_pending_exec=false;
	
	model.data.sd.update=function(model,op,results)
	{
		local result;
		if(results==nil)
		{
			result=this.last_result[op.pdata_index];
		}
		else
		{
			result=unpack(results);
			this.last_result[op.pdata_index]=result;
		}

		
		if(is_nil(result))
		{
			model.data.result_type=9;
		}
		else if(is_table(result))
		{
			model.data.result_type=9;
		}
		else if(is_string(result))
		{
			model.data.result_string=result;
			model.data.result_type=1;
		}
		else if(is_array(result))
		{
			model.data.result_array=result;
			model.data.result_type=2;
		}		
		else if(typename(result)=="DFigure")
		{
			try
			{			
				if(model.data.result_figure)
				{
					model.context.config.option.figure_attribute=model.data.result_figure.attribute;
				}
				result.attribute=model.context.config.option.figure_attribute;
			}
			catch(e)
			{
			
			}
			
			model.data.result_figure=result;
			model.data.result_type=0;
		}
		else
		{
			model.data.result_type=9;
		}
		
		model.evtmgr["result_type"].wnd_execute(14);
		model.evtmgr["result_figure"].wnd_execute(14);
		model.evtmgr["result_string"].wnd_execute(14);
		model.evtmgr["result_array"].wnd_execute(14);
	};

	local ec=model.evtmgr;
	
	local result_figure=sys.create_object("DFigure");

	ec.wm_var("op_index","op_index",0);
	ec.wm_var("result_type","result_type",9);
	ec.wm_var("result_figure","result_figure",result_figure);
	ec.wm_var("result_string","result_string","");
	ec.wm_var("result_array","result_array",[]);
	
	
	ec["result_type"].set("FLAG_HIDE_UI",true);

	local operations=this.operations;

	local on_op_selected=function(name,model)
	{
		local op_index=ec["op_index"].value;
		local ec=model.evtmgr;
		local op=operations[op_index];
		local sd=model.data.sd;

		if(name!="")
		{
			sd.update(model,op);
			model.data.result_figure=nil;
		}
		
		if(op.autoexec)
		{
			model.context.pending_exec(model,name);
		}
		
		ec["btn.help"].set("FLAG_HIDE_UI",!op.helpuri);
		ec["btn.export"].set("FLAG_HIDE_UI",!op.fn_export);		
		
		if(model.context.config.option.showstep)
		{
			model.std_execute("title",translate2(op.label));
		}

	};
	
	ec["sync_handler"].add(on_op_selected,"op_index");

	local btn_exec=ec.wm_cmd("btn.exec");
	btn_exec.do_cmd_execute=function(param,model,evtmgr)
	{
		if(!model) return true;
		
		local flag=model.transfer_data(-1);
		if(flag!=nil && !flag)
		{
			local t=table();
			
			t.title=_hT("Error");
			t.content=_hT("Transfer data from window failed!");
			t.type=262144;

			ui.evt["Dialog.Msgs"].std_execute(t);
			return false;
		}
		
		local op=operations[model.data.op_index];
		
		try
		{
			// pdata解释成目标类型
			local pdata=op.eval_pdata(model.sub_data[op.pdata_index]);
			local result=op.execute(model.context.idata,pdata,model,evtmgr);
			model.data.sd.update(model,op,[result]);
		}
		catch(...)
		{
			logger.log_message(...);
			model.data.sd.update(model,op,[]);
		}
		
		//model.transfer_data(+1);
		model.context.pending_exec(model);
		return true;
	};
	
	
	local btn_help=ec.wm_cmd(_kT("btn.help"));
	btn_help.do_cmd_execute=function(param,model,evtmgr)
	{
		if(!model) return true;
		
		model.transfer_data(-1);
		
		try
		{
			local op=operations[model.data.op_index];
			if(!is_string(op.helpuri))
			{
				return true;
			}
			
			if(op.helpuri.start_with("#"))
			{
				ec["Plugin.Help"].std_execute("document",op.helpuri.substr(1));
			}
			if(op.helpuri.start_with("?"))
			{
				ec["Plugin.Help"].std_execute("search",op.helpuri.substr(1));
			}
		}
		catch(...)
		{

		}
		return true;
	};
	
	
	local btn_export=ec.wm_cmd(_kT("btn.export"));
	btn_export.do_cmd_execute=function(param,model,evtmgr)
	{
		if(!model) return true;
		
		model.transfer_data(-1);
		
		try
		{
			local op=operations[model.data.op_index];
			local pdata=model.sub_data[op.pdata_index];
			
			if(op.fn_export)
			{
				op.fn_export(model.context.idata,pdata,model,evtmgr);
			}
		}
		catch(...)
		{

		}	
		
		return true;
	};

	
	local param_save=ec.wm_cmd(_kT("Export#sd_param"));
	param_save.do_cmd_execute=function(param,model,evtmgr)
	{
		if(!model) return true;
		model.transfer_data(-1);
		
		try
		{
			local t=table();
			t.type=2|32;
			t.exts="json files(*.json)|*.json";
			t.title=_hT("Export parameters to json file");
			ui.evt["Dialog.File"].std_execute(t);
			if(t.file==nil)
			{
				return;
			}
			
			local op=operations[model.data.op_index];
			local pdata=model.sub_data[op.pdata_index];
			to_json(pdata).save(t.file);
		}
		catch(...)
		{

		}	
		
		return true;
	};

	local param_load=ec.wm_cmd(_kT("Import#sd_param"));
	param_load.do_cmd_execute=function(param,model,evtmgr)
	{
		if(!model) return true;
		model.transfer_data(-1);
		
		try
		{
			local op=operations[model.data.op_index];
			local pdata=model.sub_data[op.pdata_index];
			
			local t=table();
			t.type=1|16;
			t.exts="json files(*.json)|*.json";
			t.title=_hT("Import parameters from json file");
			ui.evt["Dialog.File"].std_execute(t);
			if(t.file==nil)
			{
				return;
			}
			
			local p_val=io.handler.json.load(t.file);
			for(k,v in pdata)
			{
				if(typeid(v)==typeid(p_val[k]))
				{
					pdata[k]=p_val[k];
				}
				else if(is_array(v) && is_array(p_val[k]))
				{
					local n=p_val[k].length();
					pdata[k].resize(n);
					for(local i=0;i<n;i++)
					{
						pdata[k][i]=p_val[k][i];
					}
				}
			}
			model.transfer_data(+1);
		}
		catch(...)
		{

		}	
		
		return true;
	};
	
	ec.gp_beg("btn.star.menu");
		ec.gp_add("Export#sd_param");
		ec.gp_add("Import#sd_param");
	ec.gp_end();
	
	local btn_update=ec.wm_cmd("btn.update");
	btn_update.do_cmd_execute=function(param,model,evtmgr)
	{
		if(!model) return true;
		
		model.transfer_data(-1);
		try
		{
			local op=operations[model.data.op_index];
			local pdata=model.sub_data[op.pdata_index];
			
			if(op.update)
			{
				op.update(model.context.idata,pdata,model,evtmgr);
				model.transfer_data(+1);
			}
		}
		catch(...)
		{

		}
		return true;
	};
	
	this.extra_evt_init(model);

};



obj.dlg_init=function(model)
{
	local km=model.maker;
	


	
	//km.ld(0).border(0).sv(0);
	km.width(70).flags(km.IWND_WITHCOLON).align(km.ALIGN_RIGHT).sv(1);
	km.proportion(1).sv(2);
	km.border(0).flags(km.IWND_EXPAND).sv(3);
	
	local option=this.config.option;

	
	local with_toolbar=this.config.option.with_toolbar;
	if(with_toolbar==nil)
	{
		with_toolbar=cfg.get_integer("/toolbox/smartdata/with_toolbar",1);
	}
	with_toolbar=integer(with_toolbar);
	
	local showtree=this.showtree;
	local sashpos=170;
	if(showtree==nil)
	{
		showtree=cfg.get_integer("/toolbox/smartdata/show_tree",1);
		sashpos=cfg.get_integer("/toolbox/smartdata/sash_position",sashpos);
	}
	
	km.win(model.context.window,km.title(model.context.title).flags(km.IWND_NO_CLOSE).sprops("icon",model.context.icon).size(900,600));
		km.win("col", km.ld(3));
			km.win("row", km.ld(3));
	
				km.add("combo"			,km.width(240).book("book1").flags(km.IWND_READONLY|km.IWND_EXPAND).name("op_index"));

				km.win("bookbase.scroll"		,km.ld(3).book("book1").flags(km.IWND_PAGEONLY|km.IWND_EXPAND).proportion(1).min_width(240).nprops("scroll_ystep",-1));
					for(v in this.operations)
					{
						v.dlg_init(model,km);
					}
				km.end();
				km.add("hline", km.flags(km.IWND_EXPAND));
				km.win("col"			,km.ld(3));
					km.add("space"		,km.proportion(1));
					
					km.add("button"		,km.name(_kT("btn.export")));
					km.add("button"		,km.name(_kT("btn.help")));
					km.add("button"		,km.name(_kT("btn.exec")));
					km.add("button"		,km.sprops("menu","btn.star.menu").label("*").width(20));				
				km.end();
		
			km.end();
			km.add("combo"			,km.book("book2").flags(km.IWND_READONLY).name("result_type"));
			km.win("bookbase"		,km.ld(3).book("book2").flags(km.IWND_PAGEONLY|km.IWND_EXPAND).proportion(1).min_width(300));
				km.add("container",km.page("page_nil").nvalue(9));
				if(with_toolbar==1)
				{
					km.win("container",km.page("page_figure").nvalue(0));
						km.win("row");
							km.add("toolbar",km.ld(3).name("tb.Figure"));
							km.win("splitter", km.ld(5).nprops("sash",?(showtree,sashpos,0)));
								km.add("dynwindow"	,km.sprops("datamodel","tree1").flags(km.IWND_EXPAND).nprops("column0.width",130));
								km.add("modelview"	,km.sprops("datamodel","tree1").name("result_figure").proportion(1).flags(km.IWND_EXPAND));
							km.end();
						km.end();
					km.end();
				}
				else if(with_toolbar==2)
				{
					km.win("container",km.page("page_figure").nvalue(0));
						km.win("row");
							km.add("toolbar",km.ld(3).name("tb.Figure.time"));
							km.win("splitter", km.ld(5).nprops("sash",?(showtree,sashpos,0)));
								km.add("dynwindow"	,km.sprops("datamodel","tree1").flags(km.IWND_EXPAND).nprops("column0.width",130));
								km.add("modelview"	,km.sprops("datamodel","tree1").name("result_figure").proportion(1).flags(km.IWND_EXPAND));
							km.end();
						km.end();
					km.end();
				}				
				else
				{
					km.win("splitter", km.nprops("sash",?(showtree,sashpos,0)).page("page_figure").nvalue(0));
						km.add("dynwindow"	,km.sprops("datamodel","tree1").flags(km.IWND_EXPAND).nprops("column0.width",130));
						km.add("modelview"	,km.sprops("datamodel","tree1").name("result_figure").proportion(1).flags(km.IWND_EXPAND));
					km.end();
				}
				km.add("stc",km.page("page_string").nvalue(1).flags(km.IWND_TE_READONLY).name("result_string").nprops("no_line_number",1));
				km.add("gridctrl",km.page("page_gridctrl").nvalue(2).name("result_array"));
			km.end();
		km.end();
	km.end();
};
return obj;


